package com.apop.hrss.home.util;

import java.util.List;
import java.util.Map;

/**
 * @author Youhai Yan
 * @version V1.0
 * @ClassName CommonInsertSQLUtil
 * @date 2021/11/19 16:06
 * @Description: 生成指定的sql语句
 */
public class SQLUtil {

    /**
     *拼接通用的插入语句
     * @param listMap 数据的集合
     * @param tableName 表名
     * @return 生成的sql语句
     */
    public static StringBuilder commonInsertSql(Map<String, Object> listMap, String tableName) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into " + tableName + " ( ID ");
        for (String key : listMap.keySet()) {
            sql.append("," + key);
        }
        sql.append(" ) values ( FUNC_NEXTID('" + tableName + "')" );
        for (String key : listMap.keySet()) {
            sql.append(" , '" + listMap.get(key) + "'");
        }
        sql.append(" ) ");
        return sql;
    }

    /**
     * 生成带参数的sql语句
     * @param params
     * @param sql
     * @return
     */
    public static String replaceWithParams(List<Object> params, String sql) {
        for(Object o:params){
            sql = sql.replaceFirst("\\?", "'" + o.toString() + "'");
        }
        return sql;
    }
}
